//
//  ScrollWheelView.m
//  blockprty
//
//  Created by Alex Shepard on 7/16/11.
//  Copyright 2011 Eye-Fi. All rights reserved.
//

#define degreesToRadian(x)  (M_PI * x / 180.0)
#define radiansToDegrees(x) (180.0 * x / M_PI)

#import "ScrollWheelView.h"
#include <math.h>


@implementation ScrollWheelView

- (void)drawRect:(CGRect)rect
{
	NSLog(@"draw rect for scroll wheel view");
	
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextClearRect(ctx, rect);
	CGContextSetRGBFillColor(ctx, 255, 0, 0, 1);
	CGContextFillRect(ctx, CGRectMake(0, 0, 10, 10));
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    
    [self.superview touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	[self.superview touchesMoved:touches withEvent:event];
	// only one object in touches set (because we're doing single touch)
	UITouch *touch = [touches anyObject];

	CGPoint touchLocation = [touch locationInView:self];
	NSLog(@"touch is %f,%f", touchLocation.x, touchLocation.y);
	
	CGPoint centerPoint = self.center;
	NSLog(@"center is %f,%f", centerPoint.x, centerPoint.y);
	
	CGPoint touchLocationComparedToCenter = CGPointMake(centerPoint.x + touchLocation.x, centerPoint.y + touchLocation.y);
	NSLog(@"touchComparedToCenter is %f,%f", touchLocationComparedToCenter.x, touchLocationComparedToCenter.y);


	float angle = [ScrollWheelView angleBetweenPoint:touchLocationComparedToCenter and:centerPoint];
	NSLog(@"angle is %f", angle);
}

	

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	[self.superview touchesEnded:touches withEvent:event];
	NSLog(@"touches ended in scrollwheelview");
}


+(CGFloat)angleBetweenPoint:(CGPoint)first and:(CGPoint)second {
	CGFloat height = second.y - first.y;
	CGFloat width = first.x - second.x;
	CGFloat rads = atan(height/width);
	return radiansToDegrees(rads);
	//degs = degrees(atan((top - bottom)/(right - left)))
}	

@end
